<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2019/2/22
 * Time: 19:16
 */

namespace App\Ordersys\Controllers\Admin;

use App\Admin\Models\AuthGroupAccessModel;
use App\Admin\Models\MemberModel;
use App\Base\Contracts\AreaInterface;
use App\Base\Controllers\Controller;
use App\Ordersys\Contracts\OrdersysOrderInterface;
use App\Ordersys\Contracts\OrdersysPaytypeInterface;
use App\Ordersys\Contracts\OrdersysShippingInterface;
use App\Ordersys\Models\OrdersysOrderModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class MyOrderController  extends Controller
{


    /**
     * 订单接口
     * @return OrdersysOrderInterface
     */
    public function getOrdersysOrderInterface(){
        return app()->make(OrdersysOrderInterface::class);
    }

    /**
     * 快递
     * @return OrdersysShippingInterface
     */
    public function getOrdersysShippingInterface(){
        return app()->make(OrdersysShippingInterface::class);
    }

    /**
     * 支付方式
     * @return OrdersysPaytypeInterface
     */
    public function getOrdersysPaytypeInterface(){
        return app()->make(OrdersysPaytypeInterface::class);
    }
    /**
     * 我的订单列表
     */
    public function myGoodsList(Request $request){
        if ($request->getMethod() == 'POST') {
            $where=function($query) use ($request){
                $data=$request->all();
                if(!empty($data["start_date"])){
                    $query->where("add_time", ">", strtotime($data["start_date"]));
                }
                if(!empty($data["end_date"])){
                    $query->where("add_time", "<", strtotime($data["end_date"]." 23:59:59"));
                }
                if(!empty($data['wx_no'])){
                    $query->where("wx_no","like",'%'.$data['wx_no'].'%');
                }
                if(!empty($data['receiver'])){
                   $query->where("receiver","like",'%'.$data['receiver'].'%');
                }
                if(!empty($data['receiver_tel'])){
                    $query->where("receiver_tel","like",'%'.$data['receiver_tel'].'%');
                }
                if(!empty($data['shipping_code'])){
                    $query->where("shipping_code","like",'%'.$data['shipping_code'].'%');
                }
                if(!empty($data['shipping_code'])){
                    $query->where("shipping_code","like",'%'.$data['shipping_code'].'%');
                }

                if(isset($data['express_type'])){
                    $query->where("express_type",$data['express_type']);
                }
                $query->where("uid",UID);
                $query->where("order_type",1);
            };
            return $this->getOrdersysOrderInterface()->getOrderListExpressList($where);
        }
        $where=function($query){
            $query->where("status",1);
        };
        $shipping_list=$this->getOrdersysShippingInterface()->getAllList($where);
      return view("ordersys.myorder.goods")->with("shippingList",$shipping_list);
    }

    /**
     * 我的订单列表
     */
    public function search(Request $request){
        if ($request->getMethod() == 'POST') {

            $where=function($query) use ($request){
                $data=$request->all();
                if(!empty($data["start_date"])){
                    $query->where("add_time", ">", strtotime($data["start_date"]));
                }
                if(!empty($data["end_date"])){
                    $query->where("add_time", "<", strtotime($data["end_date"]." 23:59:59"));
                }
                if(!empty($data['receiver'])){
                    $query->where("receiver","like",'%'.$data['receiver'].'%');
                }

                 if(!empty($data['wx_no'])){
                     $query->where("wx_no","like",'%'.$data['wx_no'].'%');
                 }
                if(!empty($data['receiver_tel'])){
                    $query->where("receiver_tel","like",'%'.$data['receiver_tel'].'%');
                }
                if(!empty($data['shipping_code'])){
                    $query->where("shipping_code","like",'%'.$data['shipping_code'].'%');
                }
                if(!empty($data['apply_status'])){
                    $query->where("apply_status",$data['apply_status']);
                }
                if(isset($data['shipping_status'])){
                    $query->where("ordersys_order_express.shipping_status",$data['shipping_status']);
                }
                $query->where("order_type",1);
                $query->whereIn("apply_status",["30","20"]);
            };
            return $this->getOrdersysOrderInterface()->getOrderListExpressList($where);
        }
        return view("ordersys.myorder.search");
    }

    /**
     * 我的充值
     */
    public function myRechargeList(Request $request){
        if ($request->getMethod() == 'POST') {

            $where=function($query) use ($request){
                $data=$request->all();
                if(!empty($data["start_date"])){
                    $query->where("add_time", ">", strtotime($data["start_date"]));
                }
                if(!empty($data["end_date"])){
                    $query->where("add_time", "<", strtotime($data["end_date"]." 23:59:59"));
                }
                if(!empty($data['wx_no'])){
                    $query->where("wx_no","like",'%'.$data['wx_no'].'%');
                }
                $pay_uid=$request->input("pay_uid");
                if(!empty($pay_uid)){
                    $query->where("pay_uid",$pay_uid);
                }
                $query->where("uid",UID);
                $query->where("order_type",2);
            };
            return $this->getOrdersysOrderInterface()->getList($where);
        }
        return view("ordersys.myorder.recharge");
    }


    public function edit(Request $request){
        if ($request->getMethod() == 'POST') {
            $data=$request->all();
            if(empty($data["pt_name"])){
                $data["pt_name"]=0;
            }
            if(empty($data["add_fen_month"])){
                $data["add_fen_month"]=0;
            }
            $data["apply_status"]='10';
            if($orderInfo=$this->getOrdersysOrderInterface()->saveData($data)){
                $data["order_id"]=$data['id'];
                if(!$this->getOrdersysOrderInterface()->updateOrderExpresData($data)){
                    throw new \Exception("操作失败");
                }
                 return array();
            }else{
                throw new \Exception("操作失败");
            }

        }
        $where=function($query){
            $query->where("status",1);
        };
        $shipping_list=$this->getOrdersysShippingInterface()->getAllList($where);
        $where=function($query){
            $query->where("status",1);
        };
        $paytype_list=$this->getOrdersysPaytypeInterface()->getAllList($where); //支付方式
        $areaList=$this->getAreaInterface()->getAreaList(["pid"=>0]);  //区域

        if (IS_ROOT) {
            $where = array();
            $where[$this->getAuthGroupAccessModel()->table.".group_id"] = 1;
            $memberList= $this->getAuthGroupAccessModel()->newQuery()
                ->select([$this->getAuthGroupAccessModel()->table.".uid",$this->getMemberModel()->table.".nickname"])
                ->leftJoin($this->getMemberModel()->table, $this->getAuthGroupAccessModel()->table . '.uid', '=', $this->getMemberModel()->table . '.uid')->where($where)->get();
        } else {
            $memberList =$this->getMemberModel()->select(["uid","nickname"])->where(array("uid" => UID))->get();
        }
        $where=function($query) use ($request){
            $id=$request->input("id");
            $query->where($this->getOrdersysOrderModel()->table.".id",$id);
        };
        $info=$this->getOrdersysOrderInterface()->getOrderListExpressInfo($where);

//print_r($info->toArray());
//exit;
        return view("ordersys.myorder.edit")->with("info",$info)->with("memberList",$memberList)->with("mid",UID)->with("areaList",$areaList)->with("shippingList",$shipping_list)->with("paytypeList",$paytype_list);;
    }
    /**
     * 请选择省区
     * @return AreaInterface
     */
    public function getAreaInterface(){
        return app()->make(AreaInterface::class);
    }

    /**
     * @return AuthGroupAccessModel
     */
    public function getAuthGroupAccessModel(){
        return app()->make(AuthGroupAccessModel::class);
    }
    /**
     * 用户
     * @return MemberModel
     */
    public function getMemberModel(){
        return app()->make(MemberModel::class);
    }
    /**
     * 订单模块
     * @return OrdersysOrderModel
     */
    public function getOrdersysOrderModel(){
        return app()->make(OrdersysOrderModel::class);
    }
    public function show(Request $request){
        $id=$request->input("id");
        if ($request->getMethod() == 'POST') {
            $where=function($query) use ($request){
                $query->where("order_id",$request->input("id"));
            };
            return $this->getOrdersysOrderInterface()->getOrderGoodsList($where);
        }
        $where=function($query) use ($id){
            $query->where($this->getOrdersysOrderModel()->table.".id",$id);
        };
        $info=$this->getOrdersysOrderInterface()->getOrderListExpressInfo($where);
        return view("ordersys.myorder.show")->with("id",$id)->with("info",$info);
    }

    public function del(Request $request){
        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'id' => 'required',
            ],["id.required"=>'删除参数id是必须的']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $id=$request->input("id");
            return  $this->getOrdersysOrderInterface()->deleteOrderData($id);
        }
    }

    /**
     * 删除
     */
    public function delRecharge(Request $request){
        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'id' => 'required',
            ],["id.required"=>'删除参数id是必须的']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $where=function($query) use ($request){
                $id=$request->input("id");
                $query->where("id",$id);
            };
            return  $this->getOrdersysOrderInterface()->deleteData($where);
        }
    }

}
